package com.giacom.docx4j.test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBElement;

import org.docx4j.docProps.core.CoreProperties;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.Parts;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.vml.CTShape;
import org.docx4j.vml.CTTextbox;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.CTMarkupRange;
import org.docx4j.wml.P;
import org.docx4j.wml.PPr;
import org.docx4j.wml.Pict;
import org.docx4j.wml.R;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tc;
import org.docx4j.wml.Tr;

public class LeituraDocx {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, Exception {

		try {
			WordprocessingMLPackage pk = WordprocessingMLPackage.load(new File("resources/teste.docx"));
			
			MainDocumentPart partDoc = pk.getMainDocumentPart();
			
			
			CoreProperties cp = pk.getDocPropsCorePart().getJaxbElement();
			
			try {
				System.out.println(cp.getTitle().getValue().getContent().get(0));
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			Parts parts = pk.getParts();
			Map<PartName, Part> map = parts.getParts();
			System.out.println("\nparts:\n");
			for (Part part : map.values()) {
				System.out.println(part.getPartName());
			}
			
			System.out.println("\ndocument:\n");
			
			System.out.println("MainDocumentPart = " + partDoc.getPartName().getName());
			
			//Document d = partDoc.getJaxbElement();
			
			//TextUtils.extractText(d, new FileWriter("C:/temp/file.txt"));
			
			int tableNum = 0;
			
			//List<Object> a = d.getBody().getContent();
			List<Object> content = partDoc.getContent();
			
			for (Object object : content) {
				//cada linha é um objeto P
				if(object instanceof P){
					P p = (P) object;
					//System.out.println("\nP code: " + p.hashCode());
					System.out.println("P text: " + p);
					
					List<Object> cont = p.getContent();
					for (Object object2 : cont) {
						//System.out.println("p content: " + object2);
						if(object2 instanceof R){
							R r = (R) object2;
							List<Object> x1=  r.getContent();
							for (Object object3 : x1) {
								//System.out.println("object3: " + object3);
								JAXBElement<?> e1 = (JAXBElement<?>) object3;
								if(e1.getValue() instanceof Pict){
									Pict pic = (Pict) e1.getValue();
									//System.out.println(pic.getAnyAndAny());
									List<Object> x2 =  pic.getAnyAndAny();
									for (Object object4 : x2) {
										JAXBElement<?> e2 = (JAXBElement<?>) object4;
										if(e2.getValue() instanceof org.docx4j.vml.CTShape){
											org.docx4j.vml.CTShape shape = (CTShape) e2.getValue();
											//System.out.println(shape.getTitle());
											List<JAXBElement<?>> e3 = shape.getPathOrFormulasOrHandles();
											for (JAXBElement<?> jaxbElement : e3) {
												if(jaxbElement.getValue() instanceof org.docx4j.vml.CTTextbox){
													org.docx4j.vml.CTTextbox txt = (CTTextbox) jaxbElement.getValue();
													System.out.println(txt.getTxbxContent().getContent().get(0));// getEGBlockLevelElts().get(0));
												}
											}
										}
									}
								}
								
							}
							
						}
					}
					
					PPr ppr = p.getPPr();
					//System.out.println("p ppr: " + ppr);
					
					
				}else if(object instanceof JAXBElement){
					JAXBElement<?> element = (JAXBElement<?>) object;
					//System.out.println(element.getValue());
					if(element.getValue() instanceof org.docx4j.wml.Tbl){
						//tabela
						Tbl tbl = (Tbl) element.getValue();
						System.out.println("-------------------------------");
						System.out.println("Tabela " + ++tableNum + ". Linhas = " + tbl.getContent().size());
						System.out.println("-------------------------------");
						int tableLine = 0;
						for (Object line : tbl.getContent()) {
							//linha
							//System.out.println(object2);
							
							if(line instanceof org.docx4j.wml.Tr){
								Tr tr = (Tr) line;
								System.out.println("Linha " + ++tableLine + ". Colunas = " + tr.getContent().size());
								
								int tableCol = 0;
								for (Object col : tr.getContent()) {
									//coluna
									//System.out.println(col);	
									JAXBElement colElement = (JAXBElement) col; 
									if(colElement.getValue() instanceof org.docx4j.wml.Tc){
										Tc tc = (Tc) colElement.getValue();
										System.out.print("   Coluna " + ++tableCol + " = ");
										for (Object object2 : tc.getContent()) {
											System.out.println(object2);
										}
									}else{
										//outro elemento
										System.out.println("**** verificar_y: " + object);
									}
								}
							}else{
								//outro elemento
								System.out.println("**** verificar_x: " + object);
							}
						}
					}else{
						//outro elemento
						System.out.println("**** verificar1: " + object);
						
						JAXBElement<?> element1 = (JAXBElement<?>) object;
						System.out.println(element1.getValue());
						
						if(element1.getValue() instanceof CTBookmark){
							CTBookmark book = (CTBookmark) element.getValue();
							System.out.println(book.getId() + " - " + book.getName()); 
						}
						
						if(element1.getValue() instanceof CTMarkupRange){
							CTMarkupRange range = (CTMarkupRange) element1.getValue();
							System.out.println(range);
						}
						
					}
				}else{
					//outro elemento
					System.out.println("**** verificar2: " + object);
				}
			}
		} catch (Docx4JException e) {
			e.printStackTrace();
		}
		
	}

}
